package com.best.onegous;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.best.onegous.Rewrite.ObservableScrollView;
import com.best.onegous.Rewrite.OnScrollChangedCallback;
import com.best.onegous.View.PullPushLayouts;
import com.best.onegous.View.PullPushLayouts.OnTouchEventMoveListenre;
import com.best.onegous.agreement.ApiInterface;
import com.best.onegous.agreement.Apps;
import com.best.onegous.agreement.Common;
import com.best.onegous.details.Address;
import com.best.onegous.details.LoGo;
import com.best.onegous.details.MyShaiDan;
import com.best.onegous.details.MyWebView;
import com.best.onegous.details.News;
import com.best.onegous.details.Order;
import com.best.onegous.details.Red;
import com.best.onegous.details.SetUser;
import com.best.onegous.details.Setting;
import com.best.onegous.details.Suggestions;
import com.best.onegous.details.Wallet;
import com.best.onegous.details.Winning;
import com.best.onegous.details.Wish;
import com.best.onegous.http.BusinessResponse;
import com.best.onegous.http.HttpClients;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class My extends Fragment implements OnClickListener, BusinessResponse {

	private LayoutInflater inflater;

	private PullPushLayouts scrollView;
	private ImageView userimg;

	private int alphaMax = 180;
	private Intent intent;
	private DisplayMetrics metric;
	private RelativeLayout top;
	private TextView title, dz;
	private HttpClients httpclient;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		this.inflater = inflater;
		View view = inflater.inflate(R.layout.activity_my, container, false);
		view.findViewById(R.id.logo).setOnClickListener(
				new ItemListener(R.id.logo));
		view.findViewById(R.id.setting).setOnClickListener(this);
		view.findViewById(R.id.qianbao).setOnClickListener(
				new ItemListener(R.id.qianbao));
		view.findViewById(R.id.hongbao).setOnClickListener(
				new ItemListener(R.id.hongbao));
		view.findViewById(R.id.dingdan).setOnClickListener(
				new ItemListener(R.id.dingdan));
		view.findViewById(R.id.winning).setOnClickListener(
				new ItemListener(R.id.winning));
		view.findViewById(R.id.shaidan).setOnClickListener(
				new ItemListener(R.id.shaidan));
		view.findViewById(R.id.wish).setOnClickListener(
				new ItemListener(R.id.wish));
		view.findViewById(R.id.shouhuodizhi).setOnClickListener(
				new ItemListener(R.id.shouhuodizhi));
		view.findViewById(R.id.suggestions).setOnClickListener(this);
		view.findViewById(R.id.help).setOnClickListener(this);
		view.findViewById(R.id.news).setOnClickListener(
				new ItemListener(R.id.news));
		top = (RelativeLayout) view.findViewById(R.id.top);
		top.getBackground().setAlpha(0);
		title = (TextView) view.findViewById(R.id.title);
		title.setAlpha(0);
		scrollView = (PullPushLayouts) view.findViewById(R.id.scrollView);
		scrollView.setOnTouchEventMoveListenre(new OnTouchEventMoveListenre() {

			@Override
			public void onSlideUp(int mOriginalHeaderHeight, int mHeaderHeight) {

			}

			@Override
			public void onSlideDwon(int mOriginalHeaderHeight, int mHeaderHeight) {

			}

			@Override
			public void onSlide(int alpha) {
				int alphaReverse = alphaMax - alpha;
				if (alphaReverse < 0) {
					alphaReverse = 0;
				}
				top.getBackground().setAlpha(alpha);
				title.setAlpha(alpha);
				// bgNavBarDrawable.setAlpha(alpha);
				// bglineNavBarDrawable.setAlpha(alpha);

			}
		});
		dz = (TextView) view.findViewById(R.id.dz);
		userimg = (ImageView) view.findViewById(R.id.userimg);

		httpclient = new HttpClients(getActivity());
		httpclient.addResponseListener(this);
		return view;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		if ("".equals(Common.code_id)) {
			dz.setText("登陆/注册");
			userimg.setImageResource(R.drawable.touxiang3);

		} else {

			RequestParams params = new RequestParams();
			params.add("user_id", Common.user_id);
			params.add("code_id", Common.code_id);
			httpclient.httpclient(ApiInterface.YONGHUXINXI, params, false);

		}

	}

	private class ItemListener implements View.OnClickListener {
		private int id;

		public ItemListener(int id) {
			this.id = id;
		}

		@Override
		public void onClick(View v) {
			if ("".equals(Common.code_id)) {
				intent = new Intent(getActivity(), LoGo.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_buttom_in,
						R.anim.push_buttom_out);
			} else {

				switch (id) {
				case R.id.logo:
					intent = new Intent(getActivity(), SetUser.class);
					getActivity().startActivity(intent);

					break;

				case R.id.qianbao:
					intent = new Intent(getActivity(), Wallet.class);
					getActivity().startActivity(intent);
					break;
				case R.id.hongbao:
					intent = new Intent(getActivity(), Red.class);
					getActivity().startActivity(intent);
					break;
				case R.id.dingdan:
					intent = new Intent(getActivity(), Order.class);
					getActivity().startActivity(intent);
					break;
				case R.id.winning:
					intent = new Intent(getActivity(), Winning.class);
					getActivity().startActivity(intent);
					break;
				case R.id.shaidan:
					intent = new Intent(getActivity(), MyShaiDan.class);
					getActivity().startActivity(intent);
					break;
				case R.id.wish:
					intent = new Intent(getActivity(), Wish.class);
					getActivity().startActivity(intent);
					break;
				case R.id.shouhuodizhi:
					intent = new Intent(getActivity(), Address.class);
					getActivity().startActivity(intent);
					break;

				case R.id.news:
					intent = new Intent(getActivity(), News.class);
					getActivity().startActivity(intent);
					break;
				}
			}
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		case R.id.setting:
			intent = new Intent(getActivity(), Setting.class);
			getActivity().startActivity(intent);
			break;

		case R.id.suggestions:
			intent = new Intent(getActivity(), Suggestions.class);
			getActivity().startActivity(intent);
			break;

		case R.id.help:
			intent = new Intent(getActivity(), MyWebView.class);
			intent.putExtra("title", "帮助");
			getActivity().startActivity(intent);
			break;

		}
	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo)
			throws JSONException {
		if (null != jo) {
			if (url.equals(ApiInterface.YONGHUXINXI)) {
				JSONObject data = jo.getJSONObject("data");
				dz.setText(data.getString("username"));
				Apps.imageLoader.displayImage(Common.PHOTOURL+data.getString("img"), userimg, Apps.touxiang);
			}
		}
	}

}
